{extend name="layout/default" /}


{block name="head"}
<title>添加</title>
{/block}
{block name="content"}
<div class="ok-body">
    <!--form表单-->
    <form class="layui-form layui-form-pane ok-form">

        <div class="layui-form-item">
            <label class="layui-form-label">上级菜单</label>
            <div class="layui-input-block">
                <select name="pid">
                    <option value="0">顶级菜单</option>
                    {$menus|raw}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单名称</label>
            <div class="layui-input-block">
                <input type="text" name="title" autocomplete="off" class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">链接</label>
            <div class="layui-input-block">
                <input type="text" name="name" placeholder="模块名/控制器名/方法名" autocomplete="off" class="layui-input"
                    lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">图标</label>
            <div class="layui-input-block">
                <input type="text" id="icon" name="icon" value="fa-arrows" lay-filter="iconPicker2" class="hide">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否菜单</label>
            <div class="layui-input-block">
                <input type="checkbox" name="ismenu" lay-skin="switch" lay-text="是|否" checked value="1">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="checkbox" name="status" lay-skin="switch" lay-text="启用|停用" checked value="1">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权重</label>
            <div class="layui-input-block">
                <input type="text" name="weigh" placeholder="越小越靠前" autocomplete="off" class="layui-input" value="100">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input type="text" name="remark" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="add">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
{/block}
{block name="scripts"}
<!--js逻辑-->
<!--<script src="/static/lib/layui/lay/lay-config.js"></script>-->
<script>
layui.use(["form", "laydate", "icon",'jquery'], function () {
   let form = layui.form;
        let $ = layui.jquery;
        let laydate = layui.laydate;
        let icon = layui.icon;

   
        icon.render({
            // 选择器，推荐使用input
            elem: "#icon",
            // fa 图标接口
            //url: "/static/css/font-awesome/less/variables.less",
            // 是否开启搜索：true/false
            search: true,
            placeholder: '',
            isSplit: true,
            // 是否开启分页
            page: true,
            // 每页显示数量，默认12
            limit: 12,
            // 点击回调
            // click: function (data) {
            //     $("#icon").val(data.field.icon)
            // },
            click: function (data) {
            },
            ready: function(d) {
            },
            // 渲染成功后的回调
            success: function (d) {
                console.log(d);
            }
        });
        //laydate.render({elem: "#birthday", type: "datetime"});

        form.verify({

        });

         form.on("submit(add)", function (data) {
               if(data.field.icon){
                        data.field.icon = 'layui-icon '+ data.field.icon;
                    }
            $.ajax(
                {
                    url:"{:url('add')}",
                    type:"post",
                    data:data.field,
                }
                ).done(function (response) {
                //console.log(response);
                if (response.code == 1) {
                    layer.msg(response.msg,{icon:1,time:1000},function(){
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                         parent.location.reload();
                    });
                } else {
                    layer.msg(response.msg,{icon:2,time:1000},function(){

                    })
                }

            }).fail(function (error) {
                console.log(error)
            });
            return false;
        });
    });
</script>


<script>
    layui.use('upload', function () {
        var $ = layui.jquery,
            upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1',
            url: '{:url("upload/index")}' //改成您自己的上传接口
            ,
            before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            },
            done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                console.log(res);
                //上传成功
                $('input[name=avatar]').val(res.path);
            },
            error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });


    });
</script>
{/block}